package FTP;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;


import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

public class FTPClientExample {

	public static final String USAGE = "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n"
			+ "\nDefault behavior is to download a file and use ASCII transfer mode.\n"
			+ "\t-s store file on server (upload)\n"
			+ "\t-b use binary transfer mode\n";

	public static String Ftpvoid(String remote, String local, String directorio, String directorio2, String directorio3) {
		int base = 0;
		boolean storeFile = false, binaryTransfer = false, error = false;
		String server, username, password;
		FTPClient ftp;

		storeFile = true;

		binaryTransfer = true;

		
		server = "10.10.119.110";
		int port = 21;
		username = "ftpflv";
		password = "qwe321";
		
		
		ftp = new FTPClient();
		ftp.addProtocolCommandListener(new PrintCommandListener(
				new PrintWriter(System.out)));

		try {
			int reply;
			ftp.connect(server, port);
			System.out.println("Connected to " + server + ".");

			// After connection attempt, you should check the reply code to
			// verify
			// success.
			reply = ftp.getReplyCode();

			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				System.err.println("FTP server refused connection.");
				System.exit(1);
			}
		} catch (IOException e) {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
			System.err.println("Could not connect to server.");
			e.printStackTrace();
			System.exit(1);
		}

		__main: try {
			if (!ftp.login(username, password)) {
				ftp.logout();
				error = true;
				break __main;
			}

			System.out.println("Remote system is " + ftp.getSystemName());

			if (binaryTransfer)
				ftp.setFileType(FTP.BINARY_FILE_TYPE);

			// Use passive mode as default because most of us are
			// behind firewalls these days.
			ftp.makeDirectory(directorio);
			ftp.makeDirectory(directorio2);
			ftp.makeDirectory(directorio3);
			
			ftp.enterLocalPassiveMode();

			if (storeFile) {
				InputStream input;

				input = new FileInputStream(local);

				ftp.storeFile(remote, input);

				input.close();
			} else {
				OutputStream output;

				output = new FileOutputStream(local);

				ftp.retrieveFile(remote, output);

				output.close();
			}

			ftp.logout();
		} catch (FTPConnectionClosedException e) {
			error = true;
			System.err.println("Server closed connection.");
			e.printStackTrace();
		} catch (IOException e) {
			error = true;
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
		}

		//System.exit(error ? 1 : 0);
		return local;
	} // end main

}
